/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

//
//  AppDelegate.m
//  快来运
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "EBForeNotification.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application      didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{ 
    //------------------------------JMessage start----------------------------------- 
    _jmessage = [JMessageHelper new]; 
    [_jmessage initJMessage:launchOptions]; 
    //------------------------------JMessage end-----------------------------------
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES] ;

    //------------------------------JMessage start-----------------------------------
    
//JMessage remove code mark;
    
//JMessage remove code mark;
    //------------------------------JMessage end-----------------------------------

    [ [ UIApplication sharedApplication] setIdleTimerDisabled:YES ] ;

    //------------------------------JMessage start-----------------------------------

//JMessage remove code mark;

//JMessage remove code mark;
    //------------------------------JMessage end-----------------------------------
    //------------------------------JMessage start-----------------------------------

//JMessage remove code mark;

//JMessage remove code mark;
    //------------------------------JMessage end-----------------------------------
    //------------------------------JMessage start-----------------------------------

//JMessage remove code mark;

//JMessage remove code mark;
    //------------------------------JMessage end-----------------------------------
    
    self.viewController = [[MainViewController alloc] init];

    if (launchOptions) {

        NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
        if(url) {

            NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:[self parseQueryString: [url query]]];
            self.viewController.startPage = [@"/index.html#" stringByAppendingString:paramsDic[@"path"]];
        }
    }
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

//- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
//    
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"你唤醒了您的应用" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
//    [alertView show];
//    
//    if(url) {
//                
//        NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:[self parseQueryString: [url query]]];
//
//        // 接受传过来的参数
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"
//                                                                    message:paramsDic[@"path"]
//                                                                   delegate:nil
//                                                          cancelButtonTitle:@"OK"
//                                                          otherButtonTitles:nil];
//        [alertView show];
//        
//        self.viewController.startPage = @"/index.html#/user/login/";
//        
//        self.window.rootViewController = self.viewController;
//        [self.window makeKeyAndVisible];
//    }
//    
//    return NO;
//}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    [JPUSHService registerDeviceToken:deviceToken]; 

//JMessage remove code mark;

//JMessage remove code mark;

//JMessage remove code mark;

//JMessage remove code mark;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
    [JPUSHService handleRemoteNotification:userInfo]; 
    [EBForeNotification handleRemoteNotification:userInfo soundID:1312];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
    [_jmessage didReceiveRemoteNotification:userInfo];
    [EBForeNotification handleRemoteNotification:userInfo soundID:1312];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}

- (NSDictionary *)parseQueryString:(NSString *)query
{
    NSMutableDictionary *paramDict = [[NSMutableDictionary alloc] initWithDictionary:0];
    
    NSArray *paramArr = [query componentsSeparatedByString:@"&"];
    for (NSString *param in paramArr)
    {
        NSArray * elements = [param componentsSeparatedByString:@"="];
        if ([elements count] <= 1)
        {
            return nil;
        }
        
        NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *value = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [paramDict setObject:value forKey:key];
    }
    
    return paramDict;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
    // NSUserActivityTypeBrowsingWeb 由Universal Links唤醒的APP
    if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSURL *url = userActivity.webpageURL;
        if(url) {
            NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:[self parseQueryString: [url query]]];
            self.viewController.startPage = [@"/index.html#" stringByAppendingString:paramsDic[@"path"]];
        }
    }
    return YES;
}
@end
